//
//  MyDateDetailVC.m
//  MovieDate
//
//  Created by 杨运涛 on 16/3/27.
//  Copyright © 2016年 MovieDate. All rights reserved.
//

#import "MyDateDetailVC.h"
#import "TDJScrollAdView.h"
#import <MessageUI/MessageUI.h>

@interface MyDateDetailVC () <TDJScrollViewDataSource, TDJScrollViewDelegate, MFMessageComposeViewControllerDelegate, UIAlertViewDelegate> {
    TDJScrollAdView *srcView;
    
    NSMutableArray *pArrayUserPic;
}

@property (weak, nonatomic) IBOutlet UILabel *pLblName;
@property (weak, nonatomic) IBOutlet UIImageView *pImgSex;
@property (weak, nonatomic) IBOutlet UILabel *pLblAge;
@property (weak, nonatomic) IBOutlet UILabel *pLblMovieName;
@property (weak, nonatomic) IBOutlet UILabel *pLblPhone;

@property (weak, nonatomic) IBOutlet UIView *pViewHeader;

@end

@implementation MyDateDetailVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)initInfo
{
    [super initInfo];
    NSLog(@"%@",self.pDict);
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self selDefaultNavigationBackground];
    
    [_pLblName setText:[_pDict objectForKey:@"nickname"]];
    [_pImgSex setImage:[UIImage imageNamed:[[_pDict objectForKey:@"userSex"] isEqualToString:@"1"]?@"sex_boy":@"sex_girl"]];
    [_pLblAge setText:[_pDict objectForKey:@"userAge"]];
    [_pLblMovieName setText:[_pDict objectForKey:@"title"]];
    pArrayUserPic = [NSMutableArray array];
    [pArrayUserPic addObject:[NSString stringWithFormat:@"%@%@", IP, [_pDict objectForKey:@"headImage"]]];
    [self setStrID:[_pDict objectForKey:@"toUid"]];
    
    [self.pViewHeader setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width)];
    self.pTableView.tableHeaderView = self.pViewHeader;
    srcView = [[TDJScrollAdView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width)];
    [srcView setDataSource:self];
    [srcView setDelegate:self];
    [srcView.timerScroll invalidate];
    [self.pViewHeader addSubview:srcView];
    [srcView reloadData];
}

- (IBAction)btnPopAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setStrID:(NSString *)strID
{
    [self requestUserAlbum:strID];
    [TDNetwork requsetUseMessagePostUrl:useGetMessageIP uid:strID complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
        if (succeed) {
            
            NSDictionary *dic = [dictionary valueForKey:@"res"];
            NSLog(@" 用户信息： %@",dic);
            User *user = [User defaultUser];
            if (dic) {
                user.uid            = [NSString stringWithFormat:@"%@",[dic valueForKey:@"uid"]];
                user.headImage      = [NSString stringWithFormat:@"%@",[dic valueForKey:@"headImage"]];
                user.nickName       = [NSString stringWithFormat:@"%@",[dic valueForKey:@"nickName"]];
                user.userAge        = [NSString stringWithFormat:@"%@",[dic valueForKey:@"userAge"]];
                user.userSex        = [NSString stringWithFormat:@"%@",[dic valueForKey:@"userSex"]];
                user.zambia         = [NSString stringWithFormat:@"%@",[dic valueForKey:@"zambia"]];
                user.faceScore      = [NSString stringWithFormat:@"%@",[dic valueForKey:@"faceScore"]];
                user.fans           = [NSString stringWithFormat:@"%@",[dic valueForKey:@"fans"]];
                user.movieLabel     = [NSString stringWithFormat:@"%@",[dic valueForKey:@"movieLabel"]];
                user.commentCount   = [NSString stringWithFormat:@"%@",[dic valueForKey:@"commentCount"]];
                user.commentStart   = [NSString stringWithFormat:@"%@",[dic valueForKey:@"commentStart"]];
                user.commentTotalScore = [NSString stringWithFormat:@"%@",[dic valueForKey:@"commentTotalScore"]];
                user.phone          = [NSString stringWithFormat:@"%@",[dic valueForKey:@"phone"]];
                user.pwd            = [NSString stringWithFormat:@"%@",[dic valueForKey:@"pwd"]];
                user.beReprotedCount= [NSString stringWithFormat:@"%@",[dic valueForKey:@"beReportedCount"]];
                user.appointmentCount = [NSString stringWithFormat:@"%@",[dic valueForKey:@"appointmentCount"]];
                user.breakCount     = [NSString stringWithFormat:@"%@",[dic valueForKey:@"breakCount"]];
                user.signature      = [NSString stringWithFormat:@"%@",[dic valueForKey:@"signature"]];
                user.address        = [NSString stringWithFormat:@"%@",[dic valueForKey:@"address"]];
                user.activeTime     = [NSString stringWithFormat:@"%@",[dic valueForKey:@"activeTime"]];
                [_pLblPhone setText:user.phone];
            }
        }
    }];
}

- (void)requestUserAlbum:(NSString *)uid
{
    [TDNetwork getUserAlbumImagePostUrl:getAlbumImageIP uid:[uid integerValue] complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
        if (succeed) {
            NSDictionary *dict = [dictionary objectForKey:@"res"];
            @try {
                NSArray *array = [dict objectForKey:@"albumList"];
                if (!pArrayUserPic) {
                    pArrayUserPic = [NSMutableArray array];
                }
                for (NSDictionary *item in array) {
                    NSLog(@"%@", item);
                    [pArrayUserPic addObject:[NSString stringWithFormat:@"%@%@", IP, [item objectForKey:@"albumId"]]];
                }
            }
            @catch (NSException *exception) {}
            @finally {
                [srcView reloadData];
            }
        }
    }];
}


#pragma mark - TDJScrollViewDataSource, TDJScrollViewDelegate

- (NSInteger)numberOfPages {
    return pArrayUserPic.count;
}

- (UIView *)pageAtIndex:(NSInteger)index
{
    UIImageView *imgPic = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, srcView.frame.size.height)];
    [imgPic sd_setImageWithURL:[NSURL URLWithString:[pArrayUserPic objectAtIndex:index]]];
    
    return imgPic;
}

- (void)didClickPage:(TDJScrollAdView *)view atIndex:(NSInteger)index {
}

- (IBAction)btnCallAction:(id)sender
{
    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@", _pLblPhone.text];
    UIWebView * callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
}

- (IBAction)btnShowMessageView:(id)sender {
    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提醒" message:@"要发送短信给对方吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"发送短信", nil];
    [alert show];
}

//MFMessageComposeViewControllerDelegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
    [controller dismissViewControllerAnimated:NO completion:^{}];
}


#pragma mark - UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        if([MFMessageComposeViewController canSendText])
        {
            MFMessageComposeViewController * controller = [[MFMessageComposeViewController alloc]init]; //autorelease];
            
            controller.recipients = [NSArray arrayWithObject:_pLblPhone.text];
            controller.body = @"";
            controller.messageComposeDelegate = self;
            [self presentViewController:controller animated:YES completion:^{}];
            [[[[controller viewControllers] lastObject] navigationItem] setTitle:@"发短信"];//修改短信界面标题
        }
    }
}
////关注接口

-(void)attentionAction:(NSString *)uid{
    [SVProgressHUD show];
    [TDNetwork  requestCollection:MY_COLLECTION_List toUid: [[_pDict valueForKey:@"toUid"] integerValue] complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
        if (succeed) {
            if ([ret isEqualToString:@"1"]) {
                [SVProgressHUD showSuccessWithStatus:@"关注成功"];
            }else{
                [SVProgressHUD showSuccessWithStatus:@"未知错误"];
            }
            }else{
            [SVProgressHUD showErrorWithStatus:@"网络请求失败"];
        }
    }];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
